<template>
  <div class="app-container">
    <!-- 查询对话框   -->
    <el-dialog
      :title="title"
      v-model="openSearch"
      :close-on-click-modal="false"
      draggable
      width="780px"
      append-to-body
    >
      <el-form
        :model="queryParams"
        ref="queryForm"
        :inline="true"
        label-width="98px"
      >
        <el-col :span="12">
          <el-form-item label="生产日期">
            <el-date-picker
              v-model="daterangeChmCkrq"
              size="small"
              value-format="yyyy-MM-dd"
              type="daterange"
              range-separator="-"
              style="width: 215px"
              start-placeholder="开始日期"
              end-placeholder="结束日期"
              :picker-options="pickerOptions"
              :default-time="['00:00:00', '23:59:59']"
            ></el-date-picker>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="制造单号" prop="chdDjbh">
            <el-input
              v-model="queryParams.chdDjbh"
              placeholder="请输入出库单号"
              clearable
              size="small"
            />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="销售单号" prop="chdSodh">
            <el-input
              v-model="queryParams.chdSodh"
              placeholder="请输入销售单号"
              clearable
              size="small"
            />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="物料代码" prop="moeWldm">
            <el-input
              v-model="queryParams.moeWldm"
              placeholder="请输入物料代码"
              clearable
              size="small"
            />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="模具编号" prop="moeMjbh">
            <el-input
              v-model="queryParams.moeMjbh"
              placeholder="请输入物料代码"
              clearable
              size="small"
            />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="机台编号" prop="moeJtbh">
            <el-input
              v-model="queryParams.moeJtbh"
              placeholder="请输入物料代码"
              clearable
              size="small"
            />
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item
            style="margin-top: 25px; display: flex; justify-content: flex-end"
          >
            <el-button @click="cancel">取消</el-button>
            <el-button type="primary" @click="handleQuery">确认</el-button>
          </el-form-item>
        </el-col>
      </el-form>
    </el-dialog>
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-search"
          @click="handleSearch"
          size="small"
          >查询</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          v-hasPermi="['output:chmAnalyse:export']"
          @click="handleExcel(1)"
          size="small"
        >
          明细数据
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          v-hasPermi="['output:chmAnalyse:export']"
          @click="handleExcel(2)"
          size="small"
        >
          物料统计
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          v-hasPermi="['output:chmAnalyse:export']"
          @click="handleExcel(3)"
          size="small"
        >
          日期统计
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          v-hasPermi="['output:chmAnalyse:export']"
          @click="handleExcel(4)"
          size="small"
        >
          公司出库
        </el-button>
      </el-col>
    </el-row>
    <el-tabs v-model="activeTab">
      <el-tab-pane label="日计划信息" name="first">
        <el-table
          border
          stripe
          :height="tableHChild"
          @scrollBottom="load"
          :data="planDetailList"
          :header-cell-style="{ 'text-align': 'center' }"
        >
          <el-table-column
            label="#"
            type="index"
            width="55"
            align="center"
            fixed="left"
          />
          <el-table-column
            label="计划日期"
            align="center"
            width="135"
            prop="pqd_scrq"
          />
          <el-table-column
            label="机台"
            align="center"
            width="135"
            prop="pqd_jtbh"
          />
          <el-table-column
            label="组别"
            align="center"
            width="135"
            prop="pqd_bbdm"
          />
          <el-table-column
            label="班次"
            align="center"
            width="135"
            prop="pqd_bcdm"
          />
          <el-table-column
            label="制造单号"
            width="135"
            align="center"
            prop="moe_zzdh"
          />
          <el-table-column
            label="销售单号"
            width="135"
            align="center"
            prop="moe_sodh"
          />
          <el-table-column
            label="物料代码"
            width="135"
            align="center"
            prop="moe_wldm"
          />
          <el-table-column
            label="品名规格"
            width="285"
            show-overflow-tooltip
            prop="itm_pmgg"
          />
          <el-table-column
            label="模具编号"
            width="285"
            show-overflow-tooltip
            prop="moe_mjbh"
          />
          <el-table-column
            label="生产数量"
            width="285"
            show-overflow-tooltip
            prop="moe_scsl"
          />
          <el-table-column
            label="前期完成"
            width="285"
            show-overflow-tooltip
            prop="pqd_prewcsl"
          />
          <el-table-column
            label="当前计划"
            width="285"
            show-overflow-tooltip
            prop="pqd_curjh"
          />
          <el-table-column
            label="计划数量"
            width="285"
            show-overflow-tooltip
            prop="pqd_jhscsl"
          />
          <el-table-column
            label="完成数量"
            width="285"
            show-overflow-tooltip
            prop="pqd_sjwcsl"
          />
          <el-table-column
            label="未完成量"
            width="285"
            show-overflow-tooltip
            prop="pqd_wwcl"
          />
          <el-table-column
            label="穴数"
            width="285"
            show-overflow-tooltip
            prop="moe_xs"
          />
          <el-table-column
            label="成型时间（s）"
            width="285"
            show-overflow-tooltip
            prop="moe_cxsj"
          />
          <el-table-column
            label="是否达成"
            width="120"
            show-overflow-tooltip
            prop="pqd_flag"
          />
          <el-table-column
            label="不计达成"
            :show-overflow-tooltip="true"
            width="125"
            align="center"
            prop="pqd_bsflag"
          >
            <template #default="scope">
              <dict-tag :options="NcountedList" :value="scope.row.pqd_bsflag" />
            </template>
          </el-table-column>
          <el-table-column
            label="转换工时（H）"
            width="80"
            align="center"
            prop="moe_kcgs"
          />
          <el-table-column
            label="开始时间"
            width="80"
            align="center"
            prop="pqd_kssj"
          />
          <el-table-column
            label="结束时间"
            width="80"
            align="center"
            prop="pqd_jssj"
          />
        </el-table>
      </el-tab-pane>
      <el-tab-pane label="日计划统计" name="second">
        <el-table
          border
          stripe
          @scrollBottom="load2"
          :height="tableHChild"
          :data="planTotalList"
        >
          <el-table-column
            label="#"
            type="index"
            width="55"
            align="center"
            fixed="left"
          />
          <el-table-column label="计划日期" align="center" prop="pqd_scrq" />
          <el-table-column label="机台" align="center" prop="moe_jtbh" />
          <el-table-column label="物料代码" align="center" prop="moe_wldm" />
          <el-table-column label="模具编号" align="center" prop="moe_mjbh" />
          <el-table-column label="计划数量" align="center" prop="pqd_jhscsl" />
          <el-table-column label="完成数量" align="center" prop="pqd_sjwcsl" />
          <el-table-column label="未完成量" align="center" prop="pqd_wwcl" />
          <el-table-column
            label="数量达成率I%"
            align="center"
            prop="pqd_rate"
          />
        </el-table>
      </el-tab-pane>
      <el-tab-pane label="达成率统计" name="third">
        <el-table
          border
          stripe
          :height="tableHChild"
          @scrollBottom="load3"
          :data="planAchieveList"
        >
          <el-table-column
            label="#"
            type="index"
            width="55"
            align="center"
            fixed="left"
          />
          <el-table-column label="计划日期" align="center" prop="TT_scrq" />
          <el-table-column label="组别" align="center" prop="TT_zbdm" />
          <el-table-column label="品名规格" align="center" prop="vPmgg" />
          <el-table-column label="班次" align="center" prop="TT_bcdm" />
          <el-table-column label="计划总数量A" align="center" prop="TT_jhsl" />
          <el-table-column label="完成总数量B" align="center" prop="TT_sjsl" />
          <el-table-column label="未完成总数量" align="center" prop="TT_wwcl" />
          <el-table-column
            label="数量达成率I%=B/A"
            align="center"
            prop="TT_rate"
          />
        </el-table>
      </el-tab-pane>
      <el-tab-pane label="图表分析" name="fifth" style="width: 100%">
        <div style="position: relative; width: 100%" ref="chart1">
          <div
            :style="{ width: widthTb + 'px', height: '400%' }"
            ref="chart"
          ></div>
        </div>
      </el-tab-pane>
    </el-tabs>
  </div>
</template>

<script>
let Echarts = require("echarts/lib/echarts"); //基础实例 注意不要使用import
// require("echarts/lib/chart/bar"); //按需引入 bar = 柱状图
require("echarts/lib/chart/line");
import { machineAchiveGet } from "@/api/produce/moeDet";

export default {
  name: "PlanReport",
  data() {
    return {
      // 遮罩层
      loading: true,
      chart: null,
      // 选中数组
      tableH: (document.body.clientHeight - 130) / 2,
      tableHChild: document.body.clientHeight - 280,
      widthTb: document.body.clientWidth - 300,
      activeTab: "first",
      planDetailList: [],
      planAchieveList: [],
      chartAnalysis: [],
      planTotalList: [],

      //不计达成字典
      NcountedList: [
        { dictValue: "0", dictLabel: "有效", listClass: "default" },
        { dictValue: "1", dictLabel: "无效", listClass: "default" },
      ],
      // 显示搜索条件
      openSearch: false,
      // 总条数
      total: 0,
      total2: 0,
      total3: 0,
      total4: 0,
      // 弹出层标题
      title: "生产计划达成率",
      // 是否显示弹出层
      open: false,
      // 建立日期时间范围
      daterangeChmCkrq: [
        new Date(new Date().getFullYear(), new Date().getMonth() - 1),
        this.$moment().endOf("month").toDate(),
      ],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 40,
        chdWldm: null,
        chdSodh: null,
        chdDjbh: null,
        chmKhdm: null,
        moeWldm: null,
        moeMjbh: null,
        moeJtbh: null,
        index: 0,
      },
      //日期初始化
      pickerOptions: {
        shortcuts: [
          {
            text: "最近一周",
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 7);
              picker.$emit("pick", [start, end]);
            },
          },
          {
            text: "最近一个月",
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 30);
              picker.$emit("pick", [start, end]);
            },
          },
          {
            text: "最近三个月",
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 90);
              picker.$emit("pick", [start, end]);
            },
          },
        ],
      },
    };
  },
  created() {
    this.getList();
  },
  mounted() {
    this.init();
  },
  methods: {
    init() {
      machineAchiveGet(this.queryParams).then((response) => {
        let rq = [];
        let rate = [];
        for (const optionElement of response.data.list4.rows) {
          for (const optionElementKey in optionElement) {
            if (optionElementKey == "TT_scrq") {
              rq.push(optionElement[optionElementKey]);
            }
            if (optionElementKey == "TT_rate") {
              rate.push(optionElement[optionElementKey]);
            }
          }
        }
        //2.初始化
        this.chart = Echarts.init(this.$refs.chart);
        //3.配置数据
        let option = {
          xAxis: {
            type: "category",
            data: rq,
          }, //X轴
          yAxis: { type: "value" }, //Y轴
          series: [{ data: rate, type: "line" }], //配置项
        };
        // 4.传入数据
        this.chart.setOption(option);
      });
    },
    /** 查询来料进货入库开单列表 */
    getList() {
      this.loading = true;
      // this.queryParams.params = {}
      // if (null != this.daterangeChmCkrq && '' != this.daterangeChmCkrq) {
      //   this.queryParams.params['beginCkTime'] = this.moment(this.daterangeChmCkrq[0]).format('YYYY-MM-DD HH:mm:ss')
      //   this.queryParams.params['endCkTime'] = this.moment(this.daterangeChmCkrq[1]).format('YYYY-MM-DD HH:mm:ss')
      // }
      machineAchiveGet(this.queryParams).then((response) => {
        this.planDetailList = [];
        this.planTotalList = [];
        this.planAchieveList = [];
        this.chartAnalysis = [];
        //日计划信息
        this.planDetailList = this.planDetailList.concat(
          response.data.list1.rows
        );
        this.total = response.data.list1.total;
        //日计划统计
        this.planTotalList = this.planTotalList.concat(
          response.data.list2.rows
        );
        this.total2 = response.data.list2.total;
        //达成率统计
        this.planAchieveList = this.planAchieveList.concat(
          response.data.list3.rows
        );
        this.total3 = response.data.list3.total;
        //图表分析
        this.chartAnalysis = this.chartAnalysis.concat(
          response.data.list4.rows
        );
        this.total4 = response.data.list4.total;
        this.loading = false;
      });
    },
    load() {
      if (
        this.queryParams.pageNum <
        Math.ceil(this.total / this.queryParams.pageSize)
      ) {
        this.queryParams.pageSize = 40;
        this.queryParams.pageNum++;
        this.listed();
      }
      return 0;
    },
    load2() {
      if (
        this.queryParams.pageNum <
        Math.ceil(this.total2 / this.queryParams.pageSize)
      ) {
        this.queryParams.pageSize = 40;
        this.queryParams.pageNum++;
        this.listed();
      }
      return 0;
    },
    load3() {
      if (
        this.queryParams.pageNum <
        Math.ceil(this.total3 / this.queryParams.pageSize)
      ) {
        this.queryParams.pageSize = 40;
        this.queryParams.pageNum++;
        this.listed();
      }
      return 0;
    },
    load5() {
      if (
        this.queryParams.pageNum <
        Math.ceil(this.total4 / this.queryParams.pageSize)
      ) {
        this.queryParams.pageSize = 40;
        this.queryParams.pageNum++;
        this.listed();
      }
      return 0;
    },
    listed() {
      machineAchiveGet(this.queryParams).then((response) => {
        //明细
        this.planDetailList = this.planDetailList.concat(
          response.data.detail.rows
        );
        this.total = response.data.detail.total;
        //物料
        this.planTotalList = this.planTotalList.concat(response.data.wldm.rows);
        this.total2 = response.data.wldm.total;
        //日期
        this.planAchieveList = this.planAchieveList.concat(
          response.data.date.rows
        );
        this.total3 = response.data.date.total;
        //仓库
        this.chartAnalysis = this.chartAnalysis.concat(response.data.cus.rows);
        this.total4 = response.data.stroage.total;
        this.loading = false;
      });
    },
    handleSearch() {
      this.openSearch = true;
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.openSearch = false;
    },

    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
      this.openSearch = false;
    },
  },
};
</script>
<style scoped></style>
